package cn.b504.qpcr.impls;

import cn.b504.qpcr.entity.report.*;
import cn.b504.qpcr.interfaces.ReportService;
import cn.b504.qpcr.utils.ConstantUtils;
import cn.b504.qpcr.utils.Docx4jUtils;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

  @Override
  public WordprocessingMLPackage generateFile(Report report) {
    //创建一个文档
    WordprocessingMLPackage wordPackage;
    try {
      wordPackage = WordprocessingMLPackage.createPackage();
    } catch (InvalidFormatException e) {
      e.printStackTrace();
      return null;
    }
    MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
    if (report.isHasLogo()) {   // 自定义 logo
      Header header = report.getHeader();
      String headerAlign = header.getHeaderAlign();
      String imgLogo = header.getImgLogo();
      if (imgLogo != null && imgLogo.length() != 0) {
        mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLogo), headerAlign, "logo", "logo"));
      }
    }
    if (report.isHasHeader()) { // 头部：标题名、日期、自定义 logo
      Header header = report.getHeader();
      String headerAlign = header.getHeaderAlign();
      String imgLogo = header.getImgLogo();
      if (imgLogo != null && imgLogo.length() != 0) {   // 自定义 logo
//        mainDocumentPart.addObject(Docx4jUtils.createLogo(wordPackage, Docx4jUtils.getPicBytes(imgLogo)));
      }
      if (header.isHasTitle()) { // 标题名
        mainDocumentPart.addObject(Docx4jUtils.createTitle(header.getTitle(), headerAlign));
      }
      if (header.isHasSubTitle()) {  // 副标题: 日期
        mainDocumentPart.addObject(Docx4jUtils.createSubTitle(header.getSubTitle(), headerAlign));
      }
    }
    if (report.isHasExperiment()) { // Experiment
      Experiment experiment = report.getExperiment();
      mainDocumentPart.addObject(Docx4jUtils.createH1("Experiment"));
      if (experiment.isHasInformation()) {    // information
        mainDocumentPart.addObject(Docx4jUtils.createH2("Information"));
        Information information = experiment.getInformation();
        mainDocumentPart.addObject(Docx4jUtils.createText("Name : ", information.getName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Barcode : ", information.getBarcode()));
        mainDocumentPart.addObject(Docx4jUtils.createText("User Name : ", information.getUserName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Instrument Name : ", information.getInstrumentName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Block Type : ", information.getBlockType()));
      }
      if (experiment.isHasExperimentSetting()) {  // experiment setting
        mainDocumentPart.addObject(Docx4jUtils.createH2("Experiment Setting"));
        ExperimentSetting experimentSetting = experiment.getExperimentSetting();
        String experimentIdentify = experimentSetting.getExperimentIdentify();
        mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Identity : ", experimentIdentify));
        /*switch (experimentIdentity) {
          case "StandardCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Standard Curve"));
            break;
          case "RelativeStandardCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Relative Quantification"));
            break;
          case "MeltCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Melt Curve"));
            break;
          default:
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", experimentIdentity));
            break;
        }*/
        mainDocumentPart.addObject(Docx4jUtils.createText("Regents Type : ", experimentSetting.getReagentsType()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Run Mode : ", experimentSetting.getRunMode()));
      }
      if (experiment.isHasParameterSetting()) {   // parameter setting
        mainDocumentPart.addObject(Docx4jUtils.createH2("Parameter Setting"));
        ParameterSetting parameterSetting = experiment.getParameterSetting();
        mainDocumentPart.addObject(Docx4jUtils.createText("Reaction Volume : ", parameterSetting.getReactionVolume() + "μL"));
        mainDocumentPart.addObject(Docx4jUtils.createText("Lid Heating Switch : ", parameterSetting.getLidHeatingSwitch()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Cover Temp : ", parameterSetting.getCoverTemp() + "℃"));
        mainDocumentPart.addObject(Docx4jUtils.createText("Lid Close Temp : ", parameterSetting.getLidCloseTemp() + "℃"));
      }
      if (experiment.isHasComments()) {   // comments
        mainDocumentPart.addObject(Docx4jUtils.createH2("Comments"));
        mainDocumentPart.addObject(Docx4jUtils.createText(experiment.getComments()));
      }
      if (experiment.isHasLoadPreview()) {    // load preview
        mainDocumentPart.addObject(Docx4jUtils.createH2("Load Preview"));
        String imgLoadPreviewPlate = experiment.getImgLoadPreviewPlate();
        if (imgLoadPreviewPlate != null && imgLoadPreviewPlate.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLoadPreviewPlate)));
        }
        String imgLoadPreviewProtocol = experiment.getImgLoadPreviewProtocol();
        if (imgLoadPreviewProtocol != null && imgLoadPreviewProtocol.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLoadPreviewProtocol)));
        }
      }
    }
    if (report.isHasPlate()) {  // plate
      Plate plate = report.getPlate();
      mainDocumentPart.addObject(Docx4jUtils.createH1("Plate edit"));
      if (plate.isHasPlateLayout()) { // plate layout
        mainDocumentPart.addObject(Docx4jUtils.createH2("Plate Edit"));
        List<List<String>> plateLayout = plate.getPlateLayout();
        if (plateLayout != null && plateLayout.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(plateLayout, true, true));
        }
      }
      if (plate.isHasTask()) {    // task
        mainDocumentPart.addObject(Docx4jUtils.createH2("Task"));
        for (String str : plate.getTask().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (plate.isHasTarget()) {  // target
        mainDocumentPart.addObject(Docx4jUtils.createH2("Target"));
        List<List<String>> target = plate.getTarget();
        if (target != null && target.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(target, true, false));
        }
      }
      if (plate.isHasSplitPlate()) {  // split plate
        mainDocumentPart.addObject(Docx4jUtils.createH2("Split Plate"));
        for (String str : plate.getSplitPlate().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (plate.isHasSample()) {  // sample
        mainDocumentPart.addObject(Docx4jUtils.createH2("Sample"));
        List<List<String>> sample = plate.getSample();
        if (sample != null && sample.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(sample, true, false));
        }
      }
      if (plate.isHasStandard()) {    // standard
        mainDocumentPart.addObject(Docx4jUtils.createH2("Standard"));
        Standard standard = plate.getStandard();
        mainDocumentPart.addObject(Docx4jUtils.createText("Select Target : ", standard.getSelectTarget()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Standard Amount : ", standard.getStandardAmount()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Standard Number : ", standard.getStandardNumber()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Factor : ", standard.getFactor()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Dilution Factor : ", standard.getDilutionFactor()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Unit : ", standard.getUnit()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Replicates : ", standard.getReplicates()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Increasing/Decreasing : ", standard.getIncreasing_decreasing()));
        mainDocumentPart.addObject(Docx4jUtils.createText("Assignment : ", ""));
        mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(ConstantUtils.AssignmentImage.get(standard.getAssignment()))));
      }
    }
    if (report.isHasProtocol()) {   // protocol
      Protocol protocol = report.getProtocol();
      mainDocumentPart.addObject(Docx4jUtils.createH1("Protocol Edit"));
      if (protocol.isHasProtocol()) { // protocol
        mainDocumentPart.addObject(Docx4jUtils.createH2("Protocol"));
        for (String str : protocol.getProtocol().split("\n")) {
          if (str.contains("\t")) {
            List<String> list = new ArrayList<>(Arrays.asList(str.split("\t")));
            List<List<String>> l = new ArrayList<>();
            l.add(list);
            mainDocumentPart.addObject(Docx4jUtils.createTable(l, false, false));
          } else {
            mainDocumentPart.addObject(Docx4jUtils.createText(str));
          }
        }
        //  advanced setting
        mainDocumentPart.addObject(Docx4jUtils.createText("Protocol graph", ""));
        String imgProtocolGraph = protocol.getImgProtocolGraph();
        if (imgProtocolGraph != null && imgProtocolGraph.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgProtocolGraph)));
        }
        for (String str : protocol.getAdvancedSetting().split("\n")) {
          if (str.contains("\t")) {
            List<String> list = new ArrayList<>(Arrays.asList(str.split("\t")));
            List<List<String>> l = new ArrayList<>();
            l.add(list);
            mainDocumentPart.addObject(Docx4jUtils.createTable(l, false, false));
          } else {
            mainDocumentPart.addObject(Docx4jUtils.createText(str));
          }
        }
      }
    }
    if (report.isHasRun()) {    // run
      Run run = report.getRun();
      mainDocumentPart.addObject(Docx4jUtils.createH1("Run"));
      if (run.isHasRunTime()) { // run time
        mainDocumentPart.addObject(Docx4jUtils.createH2("Run time"));
        for (String str : run.getRunTime().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (run.isHasTemperatureCurve()) { // run time
        mainDocumentPart.addObject(Docx4jUtils.createH2("Temperature curve"));
        String imgTemperatureCurve = run.getImgTemperatureCurve();
        if (imgTemperatureCurve != null && imgTemperatureCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgTemperatureCurve)));
        }
      }
    }
    if (report.isHasAnalysis()) {
      Analysis analysis = report.getAnalysis();
      mainDocumentPart.addObject(Docx4jUtils.createH1("Analysis"));
      if (analysis.isHasQuantification()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Quantification"));
        String imgAmplificationCurve_cq = analysis.getImgAmplificationCurve_cq();
        if (imgAmplificationCurve_cq != null && imgAmplificationCurve_cq.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Amplification curve-Cq", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAmplificationCurve_cq)));
        }
        String imgAmplificationCurve_log = analysis.getImgAmplificationCurve_log();
        if (imgAmplificationCurve_log != null && imgAmplificationCurve_log.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Amplification curve-log", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAmplificationCurve_log)));
        }
      }
      if (analysis.isHasStandardCurve()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Standard Curve"));
        String imgStandardCurve = analysis.getImgStandardCurve();
        if (imgStandardCurve != null && imgStandardCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgStandardCurve)));
        }
        List<String> formulas = analysis.getStandardCurveFormulas();
        if (formulas != null && formulas.size() != 0) {
          for (String formula : formulas) {
            mainDocumentPart.addObject(Docx4jUtils.createText(formula));
          }
        }
      }
      if (analysis.isHasQuantificationData()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Quantification data"));
        List<List<String>> quantificationData = analysis.getQuantificationData();
        if (quantificationData != null && quantificationData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(quantificationData, true, false));
        }
      }
      if (analysis.isHasMelt()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Melt curve"));
        String imgMeltCurve = analysis.getImgMeltCurve();
        if (imgMeltCurve != null && imgMeltCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Melt curve"));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgMeltCurve)));
        }
        String imgMeltPeak = analysis.getImgMeltPeak();
        if (imgMeltPeak != null && imgMeltPeak.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Melt Peak"));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgMeltPeak)));
        }
      }
      if (analysis.isHasMeltData()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Melt data"));
        List<List<String>> meltData = analysis.getMeltData();
        if (meltData != null && meltData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(meltData, true, false));
        }
      }
      if (analysis.isHasGeneExpression()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Gene expression"));
        String imgGeneExpressionBar = analysis.getImgGeneExpressionBar();
        if (imgGeneExpressionBar != null && imgGeneExpressionBar.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Bar Chart", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgGeneExpressionBar)));
        }
        mainDocumentPart.addObject(Docx4jUtils.createText("Mode : ", analysis.getGeneExpressionMode()));
        mainDocumentPart.addObject(Docx4jUtils.createText("gene expression", ""));
        List<List<String>> geneExpressionData = analysis.getGeneExpressionData();
        if (geneExpressionData != null && geneExpressionData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(geneExpressionData, true, false));
        }
      }
      if (analysis.isHasAllelic()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("Allelic"));
        String imgAllelic_cq = analysis.getImgAllelic_cq();
        if (imgAllelic_cq != null && imgAllelic_cq.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Allelic_cq", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAllelic_cq)));
        }
        String imgAllelic_rfu = analysis.getImgAllelic_rfu();
        if (imgAllelic_rfu != null && imgAllelic_rfu.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("Allelic_rfu", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAllelic_rfu)));
        }
        List<List<String>> allelicData = analysis.getAllelicData();
        if (allelicData != null && allelicData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(allelicData, true, false));
        }
      }
      if (analysis.isHasQuanlityControl()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("FailureReport"));
        mainDocumentPart.addObject(Docx4jUtils.createText("failureReport Data"));
        List<List<String>> failureReportData = analysis.getFailureReportData();
        if (failureReportData != null && failureReportData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(failureReportData, true, false));
        }
      }
      if (analysis.isHasEndPoint()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("End-Point"));
        mainDocumentPart.addObject(Docx4jUtils.createText("EndPoint Setting Data", ""));
        List<List<String>> endPointSettingData = analysis.getEndPointSettingData();
        if (endPointSettingData != null && endPointSettingData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(endPointSettingData, true, false));
        }
        mainDocumentPart.addObject(Docx4jUtils.createText("RFU Result", ""));
        List<List<String>> endPointData = analysis.getEndPointData();
        if (endPointData != null && endPointData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(endPointData, true, false));
        }
      }
    }
    return wordPackage;
  }

  @Override
  public WordprocessingMLPackage generateFileWithChinese(Report report) {
    //创建一个文档
    WordprocessingMLPackage wordPackage;
    try {
      wordPackage = WordprocessingMLPackage.createPackage();
    } catch (InvalidFormatException e) {
      e.printStackTrace();
      return null;
    }
    MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
    if (report.isHasLogo()) {   // 自定义 logo
      Header header = report.getHeader();
      String headerAlign = header.getHeaderAlign();
      String imgLogo = header.getImgLogo();
      if (imgLogo != null && imgLogo.length() != 0) {
        mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLogo), headerAlign, "logo", "logo"));
      }
    }
    if (report.isHasHeader()) { // 头部：标题名、日期、自定义 logo
      Header header = report.getHeader();
      String headerAlign = header.getHeaderAlign();
      String imgLogo = header.getImgLogo();
      if (imgLogo != null && imgLogo.length() != 0) {   // 自定义 logo
//        mainDocumentPart.addObject(Docx4jUtils.createLogo(wordPackage, Docx4jUtils.getPicBytes(imgLogo)));
      }
      if (header.isHasTitle()) { // 标题名
        mainDocumentPart.addObject(Docx4jUtils.createTitle(header.getTitle(), headerAlign));
      }
      if (header.isHasSubTitle()) {  // 副标题: 日期
        mainDocumentPart.addObject(Docx4jUtils.createSubTitle(header.getSubTitle(), headerAlign));
      }
    }
    if (report.isHasExperiment()) { // Experiment
      Experiment experiment = report.getExperiment();
      mainDocumentPart.addObject(Docx4jUtils.createH1("实验设计"));
      if (experiment.isHasInformation()) {    // information
        mainDocumentPart.addObject(Docx4jUtils.createH2("基本信息"));
        Information information = experiment.getInformation();
        mainDocumentPart.addObject(Docx4jUtils.createText("实验名称 : ", information.getName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("条码 : ", information.getBarcode()));
        mainDocumentPart.addObject(Docx4jUtils.createText("用户 : ", information.getUserName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("仪器名称 : ", information.getInstrumentName()));
        mainDocumentPart.addObject(Docx4jUtils.createText("模块类型 : ", information.getBlockType()));
      }
      if (experiment.isHasExperimentSetting()) {  // experiment setting
        mainDocumentPart.addObject(Docx4jUtils.createH2("实验设置"));
        ExperimentSetting experimentSetting = experiment.getExperimentSetting();
        String experimentIdentify = experimentSetting.getExperimentIdentify();
        mainDocumentPart.addObject(Docx4jUtils.createText("实验类型 : ", experimentIdentify));
        /*switch (experimentIdentity) {
          case "StandardCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Standard Curve"));
            break;
          case "RelativeStandardCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Relative Quantification"));
            break;
          case "MeltCurve":
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", "Melt Curve"));
            break;
          default:
            mainDocumentPart.addObject(Docx4jUtils.createText("Experiment Type : ", experimentIdentity));
            break;
        }*/
        mainDocumentPart.addObject(Docx4jUtils.createText("试剂类型 : ", experimentSetting.getReagentsType()));
        mainDocumentPart.addObject(Docx4jUtils.createText("运行模式 : ", experimentSetting.getRunMode()));
      }
      if (experiment.isHasParameterSetting()) {   // parameter setting
        mainDocumentPart.addObject(Docx4jUtils.createH2("参数设置"));
        ParameterSetting parameterSetting = experiment.getParameterSetting();
        mainDocumentPart.addObject(Docx4jUtils.createText("反应体积 : ", parameterSetting.getReactionVolume() + "μL"));
        mainDocumentPart.addObject(Docx4jUtils.createText("热盖设置 : ", parameterSetting.getLidHeatingSwitch()));
        mainDocumentPart.addObject(Docx4jUtils.createText("热盖温度 : ", parameterSetting.getCoverTemp() + "℃"));
        mainDocumentPart.addObject(Docx4jUtils.createText("热盖关闭温度 : ", parameterSetting.getLidCloseTemp() + "℃"));
      }
      if (experiment.isHasComments()) {   // comments
        mainDocumentPart.addObject(Docx4jUtils.createH2("实验描述"));
        mainDocumentPart.addObject(Docx4jUtils.createText(experiment.getComments()));
      }
      if (experiment.isHasLoadPreview()) {    // load preview
        mainDocumentPart.addObject(Docx4jUtils.createH2("加载预览"));
        String imgLoadPreviewPlate = experiment.getImgLoadPreviewPlate();
        if (imgLoadPreviewPlate != null && imgLoadPreviewPlate.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLoadPreviewPlate)));
        }
        String imgLoadPreviewProtocol = experiment.getImgLoadPreviewProtocol();
        if (imgLoadPreviewProtocol != null && imgLoadPreviewProtocol.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgLoadPreviewProtocol)));
        }
      }
    }
    if (report.isHasPlate()) {  // plate
      Plate plate = report.getPlate();
      mainDocumentPart.addObject(Docx4jUtils.createH1("孔板设置"));
      if (plate.isHasPlateLayout()) { // plate layout
        mainDocumentPart.addObject(Docx4jUtils.createH2("孔板设置"));
        List<List<String>> plateLayout = plate.getPlateLayout();
        if (plateLayout != null && plateLayout.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(plateLayout, true, true));
        }
      }
      if (plate.isHasTask()) {    // task
        mainDocumentPart.addObject(Docx4jUtils.createH2("样本类型"));
        for (String str : plate.getTask().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (plate.isHasTarget()) {  // target
        mainDocumentPart.addObject(Docx4jUtils.createH2("目标基因"));
        List<List<String>> target = plate.getTarget();
        if (target != null && target.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(target, true, false));
        }
      }
      if (plate.isHasSplitPlate()) {  // split plate
        mainDocumentPart.addObject(Docx4jUtils.createH2("分板"));
        for (String str : plate.getSplitPlate().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (plate.isHasSample()) {  // sample
        mainDocumentPart.addObject(Docx4jUtils.createH2("实验样本"));
        List<List<String>> sample = plate.getSample();
        if (sample != null && sample.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(sample, true, false));
        }
      }
      if (plate.isHasStandard()) {    // standard
        mainDocumentPart.addObject(Docx4jUtils.createH2("标准曲线"));
        Standard standard = plate.getStandard();
        mainDocumentPart.addObject(Docx4jUtils.createText("目标基因 : ", standard.getSelectTarget()));
        mainDocumentPart.addObject(Docx4jUtils.createText("起始浓度 : ", standard.getStandardAmount()));
        mainDocumentPart.addObject(Docx4jUtils.createText("数量 : ", standard.getStandardNumber()));
        mainDocumentPart.addObject(Docx4jUtils.createText("稀释因子 : ", standard.getFactor()));
        mainDocumentPart.addObject(Docx4jUtils.createText("倍比稀释 : ", standard.getDilutionFactor()));
        mainDocumentPart.addObject(Docx4jUtils.createText("浓度单位 : ", standard.getUnit()));
        mainDocumentPart.addObject(Docx4jUtils.createText("重复个数 : ", standard.getReplicates()));
        mainDocumentPart.addObject(Docx4jUtils.createText("升序/降序 : ", standard.getIncreasing_decreasing()));
        mainDocumentPart.addObject(Docx4jUtils.createText("排布方式 : ", ""));
        mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(ConstantUtils.AssignmentImage.get(standard.getAssignment()))));
      }
    }
    if (report.isHasProtocol()) {   // protocol
      Protocol protocol = report.getProtocol();
      mainDocumentPart.addObject(Docx4jUtils.createH1("程序设置"));
      if (protocol.isHasProtocol()) { // protocol
        mainDocumentPart.addObject(Docx4jUtils.createH2("程序设置"));
        for (String str : protocol.getProtocol().split("\n")) {
          if (str.contains("\t")) {
            List<String> list = new ArrayList<>(Arrays.asList(str.split("\t")));
            List<List<String>> l = new ArrayList<>();
            l.add(list);
            mainDocumentPart.addObject(Docx4jUtils.createTable(l, false, false));
          } else {
            mainDocumentPart.addObject(Docx4jUtils.createText(str));
          }
        }
        //  advanced setting
        mainDocumentPart.addObject(Docx4jUtils.createText("程序图表", ""));
        String imgProtocolGraph = protocol.getImgProtocolGraph();
        if (imgProtocolGraph != null && imgProtocolGraph.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgProtocolGraph)));
        }
        for (String str : protocol.getAdvancedSetting().split("\n")) {
          if (str.contains("\t")) {
            List<String> list = new ArrayList<>(Arrays.asList(str.split("\t")));
            List<List<String>> l = new ArrayList<>();
            l.add(list);
            mainDocumentPart.addObject(Docx4jUtils.createTable(l, false, false));
          } else {
            mainDocumentPart.addObject(Docx4jUtils.createText(str));
          }
        }
      }
    }
    if (report.isHasRun()) {    // run
      Run run = report.getRun();
      mainDocumentPart.addObject(Docx4jUtils.createH1("实时运行"));
      if (run.isHasRunTime()) { // run time
        mainDocumentPart.addObject(Docx4jUtils.createH2("运行时间"));
        for (String str : run.getRunTime().split("\n")) {
          mainDocumentPart.addObject(Docx4jUtils.createText(str));
        }
      }
      if (run.isHasTemperatureCurve()) { // run time
        mainDocumentPart.addObject(Docx4jUtils.createH2("温度曲线"));
        String imgTemperatureCurve = run.getImgTemperatureCurve();
        if (imgTemperatureCurve != null && imgTemperatureCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgTemperatureCurve)));
        }
      }
    }
    if (report.isHasAnalysis()) {
      Analysis analysis = report.getAnalysis();
      mainDocumentPart.addObject(Docx4jUtils.createH1("数据分析"));
      if (analysis.isHasQuantification()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("定量分析"));
        String imgAmplificationCurve_cq = analysis.getImgAmplificationCurve_cq();
        if (imgAmplificationCurve_cq != null && imgAmplificationCurve_cq.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("扩增曲线-Cq", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAmplificationCurve_cq)));
        }
        String imgAmplificationCurve_log = analysis.getImgAmplificationCurve_log();
        if (imgAmplificationCurve_log != null && imgAmplificationCurve_log.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("扩增曲线-log", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAmplificationCurve_log)));
        }
      }
      if (analysis.isHasStandardCurve()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("标准曲线"));
        String imgStandardCurve = analysis.getImgStandardCurve();
        if (imgStandardCurve != null && imgStandardCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgStandardCurve)));
        }
        List<String> formulas = analysis.getStandardCurveFormulas();
        if (formulas != null && formulas.size() != 0) {
          for (String formula : formulas) {
            mainDocumentPart.addObject(Docx4jUtils.createText(formula));
          }
        }
      }
      if (analysis.isHasQuantificationData()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("定量数据"));
        List<List<String>> quantificationData = analysis.getQuantificationData();
        if (quantificationData != null && quantificationData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(quantificationData, true, false));
        }
      }
      if (analysis.isHasMelt()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("熔解曲线"));
        String imgMeltCurve = analysis.getImgMeltCurve();
        if (imgMeltCurve != null && imgMeltCurve.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("熔解曲线"));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgMeltCurve)));
        }
        String imgMeltPeak = analysis.getImgMeltPeak();
        if (imgMeltPeak != null && imgMeltPeak.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("熔解曲线峰值"));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgMeltPeak)));
        }
      }
      if (analysis.isHasMeltData()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("溶解曲线数据"));
        List<List<String>> meltData = analysis.getMeltData();
        if (meltData != null && meltData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(meltData, true, false));
        }
      }
      if (analysis.isHasGeneExpression()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("基因表达分析"));
        String imgGeneExpressionBar = analysis.getImgGeneExpressionBar();
        if (imgGeneExpressionBar != null && imgGeneExpressionBar.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("基因表达分析图谱", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgGeneExpressionBar)));
        }
        mainDocumentPart.addObject(Docx4jUtils.createText("分析模式 : ", analysis.getGeneExpressionMode()));
        mainDocumentPart.addObject(Docx4jUtils.createText("基因表达分析数据", ""));
        List<List<String>> geneExpressionData = analysis.getGeneExpressionData();
        if (geneExpressionData != null && geneExpressionData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(geneExpressionData, true, false));
        }
      }
      if (analysis.isHasAllelic()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("等位基因分析"));
        String imgAllelic_cq = analysis.getImgAllelic_cq();
        if (imgAllelic_cq != null && imgAllelic_cq.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("等位基因分析-Cq", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAllelic_cq)));
        }
        String imgAllelic_rfu = analysis.getImgAllelic_rfu();
        if (imgAllelic_rfu != null && imgAllelic_rfu.length() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createText("等位基因分析-Rfu", ""));
          mainDocumentPart.addObject(Docx4jUtils.createImageInLine(wordPackage, Docx4jUtils.getPicBytes(imgAllelic_rfu)));
        }
        mainDocumentPart.addObject(Docx4jUtils.createText("等位基因分析数据", ""));
        List<List<String>> allelicData = analysis.getAllelicData();
        if (allelicData != null && allelicData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(allelicData, true, false));
        }
      }
      if (analysis.isHasQuanlityControl()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("质量控制"));
        mainDocumentPart.addObject(Docx4jUtils.createText("质量控制数据", ""));
        List<List<String>> failureReportData = analysis.getFailureReportData();
        if (failureReportData != null && failureReportData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(failureReportData, true, false));
        }
      }
      if (analysis.isHasEndPoint()) {
        mainDocumentPart.addObject(Docx4jUtils.createH2("终点法"));
        mainDocumentPart.addObject(Docx4jUtils.createText("终点法-选择与设置", ""));
        List<List<String>> endPointSettingData = analysis.getEndPointSettingData();
        if (endPointSettingData != null && endPointSettingData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(endPointSettingData, true, false));
        }
        mainDocumentPart.addObject(Docx4jUtils.createText("终点法-荧光值结果", ""));
        List<List<String>> endPointData = analysis.getEndPointData();
        if (endPointData != null && endPointData.size() != 0) {
          mainDocumentPart.addObject(Docx4jUtils.createTable(endPointData, true, false));
        }
      }
    }
    return wordPackage;
  }

  @Override
  public void export(HttpServletResponse response, WordprocessingMLPackage wordPackage, String experimentName, String type) {
    String fileName;
    // 根据时间设置文件名字
    if (experimentName.length() == 0) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh_mm_ss");
      String date = format.format(new Date());
      fileName = ("report " + date).trim();
    } else {
      fileName = experimentName;
    }
    switch (type) {
      case "docx":
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".docx");
        try {
          OutputStream outputStream = response.getOutputStream();
          Docx4jUtils.save(wordPackage, outputStream);

          outputStream.flush();
          outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
      case "pdf":
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".pdf");
        try {
          OutputStream outputStream = response.getOutputStream();
          Docx4jUtils.savePdf(wordPackage, outputStream);

          outputStream.flush();
          outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
    }
  }
}
